var http = require('http');
var querystring = require('querystring');
var URL = require('url');
var config = require('./config');

var apiBase = URL.format({
    protocol: 'http',
    hostname: 'lispy.sitemix.jp',
    port: 80,
    pathname: '/twip/o/api/'
});

function initAPI(path, method) {
    return function(options) {
        call({
            url: apiBase + path + '.' + (options.dataType || 'json'),
            method: method,
            params: options.params
        });
    };
}

var api = module.exports = {
    statuses_mentions: initAPI('statuses/mentions', 'GET'),
    statuses_public_timeline: initAPI('statuses/public_timeline', 'GET'),
    statuses_retweeted_by_me: initAPI('statuses/retweeted_by_me', 'GET'),
    statuses_retweeted_to_me: initAPI('statuses/retweeted_to_me', 'GET'),
    statuses_retweets_of_me: initAPI('statuses/retweets_of_me', 'GET'),
    statuses_user_timeline: initAPI('statuses/user_timeline', 'GET'),
    statuses_retweeted_to_user: initAPI('statuses/retweeted_to_user', 'GET'),
    statuses_retweeted_by_user: initAPI('statuses/retweeted_by_user', 'GET'),

    statuses_retweeted_by: initAPI('statuses/:id/retweeted_by', 'GET'),
    statuses_retweeted_by_ids: initAPI('statuses/:id/retweeted_by/ids', 'GET'),
    statuses_retweets: initAPI('statuses/retweets/:id', 'GET'),
    statuses_show: initAPI('statuses/show/:id', 'GET'),
    statuses_destroy: initAPI('statuses/destroy/:id', 'POST'),
    statuses_retweet: initAPI('statuses/retweet/:id', 'POST'),
    statuses_update: initAPI('statuses/update', 'POST'),
    statuses_update_with_media: initAPI('statuses/update_with_media', 'POST'),
    statuses_oembed: initAPI('statuses/oembed', 'GET'),

    search: initAPI('search', 'GET'),

    direct_messages: initAPI('direct_messages', 'GET'),
    direct_messages_sent: initAPI('direct_messages/sent', 'GET'),
    direct_messages_destroy: initAPI('direct_messages/destroy/:id', 'POST'),
    direct_messages_new: initAPI('direct_messages/new', 'POST'),
    direct_messages_show: initAPI('direct_messages/show/:id', 'GET'),

    followers_ids: initAPI('followers/ids', 'GET'),
    friends_ids: initAPI('friends/ids', 'GET'),
    friendships_exists: initAPI('friendships/exists', 'GET'),
    friendships_incoming: initAPI('friendships/incoming', 'GET'),
    friendships_outgoing: initAPI('friendships/outgoing', 'GET'),
    friendships_show: initAPI('friendships/show', 'GET'),
    friendships_create: initAPI('friendships/create', 'POST'),
    friendships_destroy: initAPI('friendships/destroy', 'POST'),
    friendships_lookup: initAPI('friendships/lookup', 'GET'),
    friendships_update: initAPI('friendships/update', 'POST'),
    friendships_no_retweet_ids: initAPI('friendships/no_retweet_ids', 'GET'),

    users_lookup: initAPI('users/lookup', 'GET'),
    users_profile_image: initAPI('users/profile_image/:screen_name', 'GET'),
    users_search: initAPI('users/search', 'GET'),
    users_show: initAPI('users/show', 'GET'),
    users_contributees: initAPI('users/contributees', 'GET'),
    users_contributors: initAPI('users/contributors', 'GET'),

    users_suggestions: initAPI('users/suggestions', 'GET'),
    users_suggestions: initAPI('users/suggestions/:slug', 'GET'),
    users_suggestions_members: initAPI('users/suggestions/:slug/members', 'GET'),

    favorites: initAPI('favorites', 'GET'),
    favorites_create: initAPI('favorites/create/:id', 'POST'),
    favorites_destroy: initAPI('favorites/destroy/:id', 'POST'),

    lists_all: initAPI('lists/all', 'GET'),
    lists_statuses: initAPI('lists/statuses', 'GET'),
    lists_members_destroy: initAPI('lists/members/destroy ', 'POST'),
    lists_memberships: initAPI('lists/memberships', 'GET'),
    lists_subscribers: initAPI('lists/subscribers', 'GET'),
    lists_subscribers_create: initAPI('lists/subscribers/create', 'POST'),
    lists_subscribers_show: initAPI('lists/subscribers/show ', 'GET'),
    lists_subscribers_destroy: initAPI('lists/subscribers/destroy', 'POST'),
    lists_members_create_all: initAPI('lists/members/create_all', 'POST'),
    lists_members_show: initAPI('lists/members/show ', 'GET'),
    lists_members: initAPI('lists/members', 'GET'),
    lists_members_create: initAPI('lists/members/create', 'POST'),
    lists_destroy: initAPI('lists/destroy', 'POST'),
    lists_update: initAPI('lists/update', 'POST'),
    lists_create: initAPI('lists/create', 'POST'),
    lists: initAPI('lists', 'GET'),
    lists_show: initAPI('lists/show', 'GET'),
    lists_subscriptions: initAPI('lists/subscriptions', 'GET'),

    account_rate_limit_status: initAPI('account/rate_limit_status', 'GET'),
    account_verify_credentials: initAPI('account/verify_credentials', 'GET'),
    account_end_session: initAPI('account/end_session ', 'POST'),
    account_update_profile: initAPI('account/update_profile ', 'POST'),
    account_update_profile_background_image: initAPI('account/update_profile_background_image', 'POST'),
    account_update_profile_colors: initAPI('account/update_profile_colors ', 'POST'),
    account_update_profile_image: initAPI('account/update_profile_image ', 'POST'),
    account_totals: initAPI('account/totals', 'GET'),
    account_settings: initAPI('account/settings', 'GET'),
    account_settings: initAPI('account/settings', 'POST'),

    notifications_follow: initAPI('notifications/follow ', 'POST'),
    notifications_leave: initAPI('notifications/leave', 'POST'),

    saved_searches: initAPI('saved_searches', 'GET'),
    saved_searches_show: initAPI('saved_searches/show/:id', 'GET'),
    saved_searches_create: initAPI('saved_searches/create ', 'POST'),
    saved_searches_destroy: initAPI('saved_searches/destroy/:id', 'POST'),

    trends: initAPI('trends/:woeid', 'GET'),
    trends_available: initAPI('trends/available', 'GET'),

    geo_id: initAPI('geo/id/:place_id ', 'GET'),
    geo_reverse_geocode: initAPI('geo/reverse_geocode ', 'GET'),
    geo_search: initAPI('geo/search', 'GET'),
    geo_similar_places: initAPI('geo/similar_places', 'GET'),
    geo_place: initAPI('geo/place', 'POST'),

    trends_daily: initAPI('trends/daily', 'GET'),
    trends_weekly: initAPI('trends/weekly', 'GET'),

    blocks_blocking: initAPI('blocks/blocking', 'GET'),
    blocks_blocking_ids: initAPI('blocks/blocking/ids', 'GET'),
    blocks_exists: initAPI('blocks/exists', 'GET'),
    blocks_create: initAPI('blocks/create', 'POST'),
    blocks_destroy: initAPI('blocks/destroy', 'POST'),

    report_spam: initAPI('report_spam', 'POST'),

    oauth_authenticate: initAPI('oauth/authenticate', 'GET'),
    oauth_authorize: initAPI('oauth/authorize', 'GET'),
    oauth_access_token: initAPI('oauth/access_token', 'POST'),
    oauth_request_token: initAPI('oauth/request_token', 'POST'),

    help_test: initAPI('help/test', 'GET'),
    help_configuration: initAPI('help/configuration', 'GET'),
    help_languages: initAPI('help/languages', 'GET'),

    legal_privacy: initAPI('legal/privacy', 'GET'),
    legal_tos: initAPI('legal/tos', 'GET')
};

function call(api, callback) {
    var params = api.params;
    var url = api.url;
    var method = api.method.toUpperCase();
    url = url.replace(/:([a-zA-Z]+)/g, function(match, param) {
        return params[param] || '';
    });
    var urlObj = URL.parse(url);
    var options = {
        method: method,
        host: urlObj.hostname,
        port: urlObj.port,
        path: urlObj.path
    };

    switch (method) {
    case 'GET':
        options.path += '?' + querystring.stringify(params);
        break;
    case 'POST':
        break;
    default:
    }

    function request(options) {
        http.request(options, function(res) {
            var data = '';
            res.setEncoding('utf8');
            res.on('error', function(err) {
                callback(err);
            });
            res.on('data', function(chunk) {
                data += chunk;
            });
            res.on('end', function() {
                callback(null, data);
            });
        });
    }

    request(options);
}

api.statuses_user_timeline({
    dateType: 'json',
    params: {
        screen_name: 'analysis918',
        cursor: -1
    }
}, function(err, data) {
    console.log(err);
    console.log(data);
});